# -*- coding:utf-8 -*-
import time
t1 = time.time()
# python 实现N个数组的排列组合(笛卡尔积算法)
class Cartesian():
    # 初始化
    def __init__(self, datagroup):
        self.datagroup = datagroup
        # 二维数组从后往前下标值
        self.counterIndex = len(datagroup)-1
        # 每次输出数组数值的下标值数组(初始化为0)
        self.counter = [0 for i in range(0, len(self.datagroup))]
        self.res = []
    # 计算数组长度
    def countlength(self):
        i = 0
        length = 1
        while(i < len(self.datagroup)):
            length *= len(self.datagroup[i])
            i += 1
        return length

    # 递归处理输出下标
    def handle(self):
        # 定位输出下标数组开始从最后一位递增
        self.counter[self.counterIndex]+=1
        # 判断定位数组最后一位是否超过长度，超过长度，第一次最后一位已遍历结束
        if self.counter[self.counterIndex] >= len(self.datagroup[self.counterIndex]):

            # 重置末位下标
            self.counter[self.counterIndex] = 0
            # 标记counter中前一位
            self.counterIndex -= 1
            # 当标记位大于等于0，递归调用
            if self.counterIndex >= 0:
                self.handle()
            # 重置标记
            self.counterIndex = len(self.datagroup)-1

    # 排列组合输出
    def assemble(self):
        length = self.countlength()
        i = 0
        while(i < length):
            attrlist = []
            j = 0
            while(j<len(self.datagroup)):
                attrlist.append(self.datagroup[j][self.counter[j]])
                j += 1
            # print(attrlist)
            self.res.append(attrlist)
            self.handle()
            i += 1

def duplicates(num_list):
    '''''
    判断一个列表中是否有重复的元素
    直接使用set方法
    '''
    if len(num_list)!=len(set(num_list)):
        return True
    else:
        return  False

def func3(i):
    return i * (i + 1) // 2


def func4(i):
    return i ** 2


def func5(n):
    '''n(3n−1)/2'''
    return n * (3 * n - 1) // 2


def func6(n):
    return n * (2 * n - 1)


def func7(n):
    return n * (5 * n - 3) // 2


def func8(n):
    return n * (3 * n - 2)


def getSeqData(func=func3):
    i = 1
    _num = []
    n = 0
    while n < 10000:
        n = func(i)
        if 1000 <= n < 10000:
            if str(n)[2] != '0':
                _num.append(int(n))
        i += 1
    return _num


def is_all_type(types):
    '''判断是否有符合的类型是全部的类型'''
    com = list(range(3,9))
    res = False
    for i in types:
        a = sorted(i)
        if a==com:
            res = True
            return res
    return res


all_nums = {}
comd_ = 'all_nums[#] = getSeqData(func=func#)'
for i in range(3, 9):
    exec_cmd = comd_.replace('#', str(i))
    exec(exec_cmd)

all_type = all_nums.keys()

aset = []
for k, v in all_nums.items():
    aset.extend(v)

#获取每个数的所属类型，注意这里一个数可能会对应多种类型
all_app = {}
distinct_aset = set(aset)
for a in distinct_aset:
    for k, v in all_nums.items():
        if a in v:
            all_app.setdefault(a, [])
            all_app[a].append(k)


def getSeqDataType(now_seq, all_app=all_app):
    datagroup1=[]
    for seq_num in now_seq:
        now_types = all_app[seq_num].copy()
        datagroup1.append(now_types)
        cartesian = Cartesian(datagroup1)
        cartesian.assemble()
        res = cartesian.res.copy()
    return [i for i in res if not duplicates(i)]

# 获取每个数的后拼接数
all_can_concat = {}
for i in distinct_aset:
    if str(i)[2:] == str(i)[:2]:
        all_can_concat.setdefault(i, [i])
    else:
        all_can_concat.setdefault(i, [])
    for v in distinct_aset:
        if v != i and str(i)[2:] == str(v)[:2]:
            all_can_concat[i].append(v)

def fun_main():
    for i1,k in  all_can_concat.items():
        now_seq = [i1]
        i1_types = all_app[i1]
        now_seq_types = []
        for j in i1_types:
            now_seq_types.append([j])
        for i2 in k:
            if len(now_seq) >= 2:
                now_seq = now_seq[:1]
                now_seq_types = getSeqDataType(now_seq)
            i2_types = all_app[i2]
            temp_types = []
            for i2_type in i2_types:
                for now_seq_type in now_seq_types:
                    tmp = now_seq_type.copy()
                    if i2_type not in now_seq_type:
                        tmp.append(i2_type)
                        temp_types.append(tmp)
            if len(temp_types)>0:
                now_seq_types = temp_types
                now_seq.append(i2)
            else:
                continue

            for i3 in all_can_concat[i2]:
                if len(now_seq) >= 3:
                    now_seq = now_seq[:2]
                    now_seq_types = getSeqDataType(now_seq)
                i3_types = all_app[i3]
                temp_types = []
                for i3_type in i3_types:
                    for now_seq_type in now_seq_types:
                        tmp = now_seq_type.copy()
                        if i3_type not in now_seq_type:
                            tmp.append(i3_type)
                            temp_types.append(tmp)
                if len(temp_types) > 0:
                    now_seq_types = temp_types
                    now_seq.append(i3)
                else:
                    continue

                for i4 in all_can_concat[i3]:
                    if len(now_seq) >= 4:
                        now_seq = now_seq[:3]
                        now_seq_types = getSeqDataType(now_seq)
                    i4_types = all_app[i4]
                    temp_types = []
                    for i4_type in i4_types:
                        for now_seq_type in now_seq_types:
                            tmp = now_seq_type.copy()
                            if i4_type not in now_seq_type:
                                tmp.append(i4_type)
                                temp_types.append(tmp)
                    if len(temp_types) > 0:
                        now_seq_types = temp_types
                        now_seq.append(i4)
                    else:
                        continue

                    for i5 in all_can_concat[i4]:
                        if len(now_seq)>=5:
                            now_seq = now_seq[:4]
                            now_seq_types = getSeqDataType(now_seq)
                        i5_types = all_app[i5]
                        temp_types = []
                        for i5_type in i5_types:
                            for now_seq_type in now_seq_types:
                                tmp = now_seq_type.copy()
                                if i5_type not in now_seq_type:
                                    tmp.append(i5_type)
                                    temp_types.append(tmp)
                        if len(temp_types) > 0:
                            now_seq_types = temp_types
                            now_seq.append(i5)
                        else:
                            continue

                        for i6 in all_can_concat[i5]:
                            if len(now_seq)==6:
                                now_seq = now_seq[:5]
                                now_seq_types = getSeqDataType(now_seq)
                            i6_types = all_app[i6]
                            temp_types = []
                            for i6_type in i6_types:
                                for now_seq_type in now_seq_types:
                                    tmp = now_seq_type.copy()
                                    if i6_type not in now_seq_type:
                                        tmp.append(i6_type)
                                        temp_types.append(tmp)
                            if len(temp_types) > 0:
                                am = now_seq.copy()
                                am.append(i6)
                                if i1 in all_can_concat[i6] and is_all_type(temp_types):
                                    now_seq.append(i6)
                                    print(sum(now_seq))
                                    return sum(now_seq)

fun_main()
print(time.time()-t1)